﻿using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;

namespace XW.Blog.EntityFrameworkCore
{
    public class BlogHttpApiHostMigrationsDbContextFactory : IDesignTimeDbContextFactory<BlogHttpApiHostMigrationsDbContext>
    {
        public BlogHttpApiHostMigrationsDbContext CreateDbContext(string[] args)
        {
            var configuration = BuildConfiguration();

            var builder = new DbContextOptionsBuilder<BlogHttpApiHostMigrationsDbContext>()
                .UseMySql(configuration.GetConnectionString("Blog"), ServerVersion.AutoDetect(configuration.GetConnectionString("Blog")));

            return new BlogHttpApiHostMigrationsDbContext(builder.Options);
        }

        private static IConfigurationRoot BuildConfiguration()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false);

            return builder.Build();
        }
    }
}
